package com.rd.student.dashuju.zhengchengxiang;

import java.util.LinkedList;

public class LinkedListTest {
    public static void main(String[] args) {
        LinkedList<Student> list = new LinkedList<>();
        // 创建对象
        Student s1 = new Student("dashuju", "1", "zhengchengxiang", "hao", 20);
        Student s2 = new Student("dashuju", "1", "sunwenzhuo", "niu", 19);
        Student s3 = new Student("dashuju", "3", "wangbo", "shua", 18);
        Student s4 = new Student("dashuju", "1", "zhengruixian", "hao", 17);
        Student s5 = new Student("wulianwang", "1", "wanggaobo", "niu", 16);
        // 添加学生信息到list中
        list.add(s1);
        list.add(s2);
        list.add(s3);
        list.add(s4);
        list.add(s5);


        System.out.println("根据班级返回list:");
        System.out.println(Student.cls(list, "3"));

        System.out.println("根据专业返回list:");
        System.out.println(Student.maj(list, "wulianwang"));

        System.out.println("根据年龄返回list:");
        System.out.println(Student.age(list, 18));

        System.out.println("所有成员名字汇总:");
        System.out.println(Student.nameCount(list));

        System.out.println("小组成员平均年龄:");
        System.out.println(Student.avg(list));

        System.out.println("筛选年龄范围:");
        System.out.println(Student.ageRange(list,16,19));


    }

    static class Student {
        private String name;
        private String classes;
        private String major;
        private String Describe;
        private int age;

        public Student() {
        }

        public Student(String major, String classes, String name, String describe, int age) {
            this.name = name;
            this.classes = classes;
            this.major = major;
            this.Describe = describe;
            this.age = age;
        }

        public String getName() {
            return name;
        }

        public String getClasses() {
            return classes;
        }

        public String getMajor() {
            return major;
        }

        public String getDescribe() {
            return Describe;
        }

        public int getAge() {
            return age;
        }

        @Override
        public String toString() {
            return "Student{" +
                    "name='" + name + '\'' +
                    ", classes='" + classes + '\'' +
                    ", major='" + major + '\'' +
                    ", Describe='" + Describe + '\'' +
                    ", age=" + age +
                    '}';
        }

        // 根据班级返回list
        public static LinkedList<Student> cls(LinkedList<Student> list, String classes) {
            LinkedList<Student> list2 = new LinkedList<>();
            for (int i = 0; i < list.size(); i++) {
                Student student = list.get(i);
                if (student.getClasses().equals(classes)) {
                    list2.add(list.get(i));
                }
            }
            return list2;
        }

        // 根据专业返回list
        public static LinkedList<Student> maj(LinkedList<Student> list, String major) {
            LinkedList<Student> list2 = new LinkedList<>();
            for (int i = 0; i < list.size(); i++) {
                Student student = list.get(i);
                if (student.getMajor().equals(major)) {
                    list2.add(list.get(i));
                }
            }
            return list2;
        }

        // 根据年龄返回list
        public static LinkedList<Student> age(LinkedList<Student> list, int age) {
            LinkedList<Student> list2 = new LinkedList<>();
            for (int i = 0; i < list.size(); i++) {
                Student student = list.get(i);
                if (student.getAge() == age) {
                    list2.add(list.get(i));
                }
            }
            return list2;
        }

        // 所有成员名字汇总
        public static StringBuilder nameCount(LinkedList<Student> list) {
            StringBuilder stringBuilder = new StringBuilder();
            stringBuilder.append("[");
            for (int i = 0; i < list.size(); i++) {
                Student student = list.get(i);
                if (i == list.size() - 1) {
                    stringBuilder.append("\"" + student.getName() + "\"").append("]");
                } else {
                    stringBuilder.append("\"" + student.getName() + "\"").append(", ");
                }
            }
            return stringBuilder;
        }

        // 计算小组平均年龄
        public static int avg(LinkedList<Student> list) {
            int averageAge = 0;
            for (int i = 0; i < list.size(); i++) {
                Student student = list.get(i);
                averageAge += student.getAge();
            }
            return averageAge /= list.size();
        }

        // 筛选年龄范围
        public static LinkedList<Student> ageRange(LinkedList<Student> list, int L, int R) {
            LinkedList<Student> list2 = new LinkedList<>();
            for (int i = 0; i < list.size(); i++) {
                if (list.get(i).getAge() >= L && list.get(i).getAge() <= R) {
                    list2.add(list.get(i));
                }
            }
            return list2;
        }
    }
}
